package com.qidi.controller;

import com.qidi.bean.Msg;
import com.qidi.bean.User;
import com.qidi.dao.UserMapper;
import com.qidi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhouXinBin
 * @create 2020-11-04-11:31
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;


    @RequestMapping("/loginpage")
    public String loginpage(){
        return "user/login";
    }

    @RequestMapping("/registpage")
    public String registpage(){
        return "user/regist";
    }

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request){

        System.out.println(user);
        User user1 = userService.login(user.getUsername(), user.getPassword());
        if (user1!=null){
            request.getSession().setAttribute("user",user1);
            return "redirect:/index";
        }else {
            request.setAttribute("msg","用户名或密码有误!");
            return "user/login";
        }
    }

@RequestMapping("/regist")
public String regist(User user, Model model){
    boolean exiet = userService.checkEixt(user.getUsername());
    if (exiet){
        userService.saveUser(user);
        return "redirect:/index";
    }else {
        model.addAttribute("msg","抱歉,用户名已存在！");
        return "user/regist";
    }


}


    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().invalidate();
        return "redirect:/index";

    }
    @RequestMapping({"/","/index"})
    public String indexpage(){
        return "index";
    }
}
